package de.breitbeil.jbooking.ui.view.tablecellrenderer;

import java.awt.Component;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

import org.springframework.beans.BeanWrapperImpl;
import org.springframework.util.StringUtils;
import org.springframework.util.comparator.NullSafeComparator;

public class ConcatenateTableCellRenderer extends DefaultTableCellRenderer implements Comparator<Object> {
    
    private List<String> properties;
    private List<String> delimiters;
    private Class<?> entity;
    private String defaultDelimiter;
    private Comparator<Object> comparator = new NullSafeComparator(String.CASE_INSENSITIVE_ORDER, false);
    
    public ConcatenateTableCellRenderer(Class<?> entity, String[] properties, String defaultDelimiter) {
        this.properties = new ArrayList<String>();
        this.delimiters = new ArrayList<String>();
        this.defaultDelimiter = defaultDelimiter;
        this.entity = entity;
        
        for(String property: properties) {
            addProperty(property, defaultDelimiter);
        }
    }

    public ConcatenateTableCellRenderer(Class<?> entity, String[] properties) {
        this(entity, properties, " ");
    }   
    
    public ConcatenateTableCellRenderer(Class<?> entity) {
        this(entity, new String[0], " ");
    }    
    
    public void addProperty(String property, String delimiter) {
        properties.add(property);
        delimiters.add(delimiter);
    }
    
    public void addProperty(String property) {
        properties.add(property);
        delimiters.add(defaultDelimiter);
    }

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        
        if(entity.isAssignableFrom(value.getClass())) {
        	
            setValue("<HTML>" + getString(value));
            
            if(table.getRowHeight(row) < getPreferredSize().getHeight()) {
                table.setRowHeight(row, (int)getPreferredSize().getHeight());
            }
        }
        
        return this;
    }
    
    public String getString(Object value) {
        if(value == null) {
            return " ";
        }
        
    	BeanWrapperImpl beanWrapper = new BeanWrapperImpl(value);
        StringBuilder builder = new StringBuilder();
        
        for(int i = 0; i < properties.size(); i++) {
            String delimiter = delimiters.get(i);
            String property = properties.get(i);
            
            if(beanWrapper.isReadableProperty(property)) {
                Object propertyValue = beanWrapper.getPropertyValue(property);
                
                if(propertyValue != null && StringUtils.hasText(propertyValue.toString())) {
                    builder.append(propertyValue.toString() + delimiter);
                }
            }
        }
        
        return builder.toString();
    }
    
	public int compare(Object o1, Object o2) {
	    return comparator.compare(getString(o1), getString(o2));
	}
}
